const Base = require('./base.js');

module.exports = class extends Base {
    /**
     *  门店列表
     * @returns {Promise<any>
     */
    async listAction() {
        const d = this.post();
        let area_name = d.city.substr(0, 2);
        const result = await this.model('shops').where({area_name: ['like', `%${area_name}%`]}).page(d.page || 1, d.limit || 30).countSelect();
        if (result.count === 0 && result.currentPage === 1) {
            const result = await this.model('shops').page(d.page || 1, d.limit || 30).countSelect();
            return this.success(result);
        }
        return this.success(result);
    }

    /**
     * 地图加载
     * @returns {Promise<any | void>}
     */
    async listsAction() {
        const result = await this.model('shops').select();
        return this.success(result);
    }

    /**
     *  门店搜索列表
     * @returns {Promise<any>
     */
    async searchAction() {
        const d = this.post();
        const result = await this.model('shops').where({shop_name: ['like', `%${d.sw}%`]}).page(d.page || 1, d.limit || 30).countSelect();
        return this.success(result);
    }

    async areaAction() {
        const result =await this.model('shops').field('distinct area_name as city').select();
        return this.success(result);
    }
};
